package com.github.ylt.controller;


import com.github.ylt.annotations.Action;
import com.github.ylt.annotations.ActionParam;
import com.github.ylt.annotations.SOAPService;
import com.github.ylt.player.Player;
import com.github.ylt.utils.XmlUtils;

import java.util.HashMap;
import java.util.Map;

@SOAPService("/AVTransport/action")
public class AVTransportController {

    private final Player instance = Player.getInstance();

    @Action(value = "SetAVTransportURI",namespace = "urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI")
    public String SetAVTransportURI(@ActionParam("InstanceID") String InstanceID,
                       @ActionParam("CurrentURI") String CurrentURI,
                       @ActionParam("CurrentURIMetaData")String CurrentURIMetaData){

        Map<String, Object> stringObjectMap = XmlUtils.getInstance().parseMateData(CurrentURIMetaData);
        Object o = stringObjectMap.get("dc:title");
        instance.setUrl(CurrentURI);
        instance.setTitle((String) o);
        instance.start();
        return XmlUtils.getInstance().responseXml(new HashMap<>(),"SetAVTransportURI","urn:schemas-upnp-org:service:AVTransport:1");
    }

    @Action(value = "GetVolume",namespace = "urn:schemas-upnp-org:service:AVTransport:1#GetVolume")
    public String GetVolume(@ActionParam("InstanceID") String InstanceID){

        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        instance.volume();
        String commandResult = instance.getCommandResult(Player.CommandType.VOLUME);
        if(commandResult == null){
            commandResult = "100.0";
        }
        objectObjectHashMap.put("CurrentVolume", commandResult);
        return XmlUtils.getInstance().responseXml(objectObjectHashMap,"GetVolume","urn:schemas-upnp-org:service:RenderingControl:1");
    }

    @Action(value = "Stop",namespace = "urn:schemas-upnp-org:service:AVTransport:1#Stop")
    public String Stop(@ActionParam("InstanceID") String InstanceID){
        instance.stop();
        return XmlUtils.getInstance().responseXml(new HashMap<>(),"Stop","urn:schemas-upnp-org:service:AVTransport:1");
    }
    @Action(value = "Play",namespace = "urn:schemas-upnp-org:service:AVTransport:1#Play")
    public String Play(@ActionParam("InstanceID") String InstanceID){
        instance.play();
        return XmlUtils.getInstance().responseXml(new HashMap<>(),"Play","urn:schemas-upnp-org:service:AVTransport:1");
    }
    @Action(value = "GetTransportInfo",namespace = "urn:schemas-upnp-org:service:AVTransport:1#GetTransportInfo")
    public String GetTransportInfo(@ActionParam("InstanceID") String InstanceID){
        instance.getPlayTime();
        return XmlUtils.getInstance().responseXml(new HashMap<>(),"GetTransportInfo","urn:schemas-upnp-org:service:AVTransport:1");
    }
}
